/* =========================================
 *        网关子服务 --- 服务器搭建 
 * =========================================*/

#include "gateway_server.hpp"
#include "logger.hpp"
#include <bits/types/FILE.h>
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式: false-调试 true-发布");
DEFINE_string(log_file, "", "指定日志的输出文件");
DEFINE_int32(log_level, 0, "指定日志输出等级");

DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");

DEFINE_int32(http_listen_port, 8888, "HTTP服务器监听端口");
DEFINE_int32(websocket_listen_port, 8889, "Websocket服务器监听端口");

DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(file_service, "/service/file_service", "监控的子服务名称");
DEFINE_string(user_service, "/service/user_service", "监控的子服务名称");
DEFINE_string(speech_service, "/service/speech_service", "监控的子服务名称");
DEFINE_string(friend_service, "/service/friend_service", "监控的子服务名称");
DEFINE_string(transmit_service, "/service/transmit_service", "监控的子服务名称");
DEFINE_string(message_service, "/service/message_service", "监控的子服务名称");

DEFINE_string(redis_host, "127.0.0.1", "Redis服务器的地址");
DEFINE_int32(redis_port, 6379, "Redis服务端口");
DEFINE_int32(redis_db, 0, "Redis默认库号");
DEFINE_bool(redis_keep_alive, true, "Redis长连接保活选项");

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    shared::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(settings);

    im_server::GatewayServerBuilder gatewayServerBuilder;
    gatewayServerBuilder.makeRedisObject(
        FLAGS_redis_host, FLAGS_redis_port, FLAGS_redis_db, FLAGS_redis_keep_alive
    );
    gatewayServerBuilder.makeRpcSvrDiscoverer(      // 构造服务发现对象, 进行服务监听
        FLAGS_registry_host, FLAGS_base_service, 
        FLAGS_file_service, FLAGS_user_service, FLAGS_speech_service,
        FLAGS_friend_service, FLAGS_transmit_service, FLAGS_message_service
    );
    gatewayServerBuilder.setNetworkPort(FLAGS_websocket_listen_port, FLAGS_http_listen_port);
    im_server::GatewayServerPtr userServer = gatewayServerBuilder.build();
    userServer->start();  
    return 0;
}